package com.example.product.server.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.product.common.ProductInfoOutput;
import com.example.product.server.utils.ResultVOUtil;
import com.example.product.server.vo.ProductInfoVo;
import com.example.product.server.vo.ProductVo;
import com.example.product.server.vo.ResultVO;
import com.google.common.collect.Lists;

@RestController
@RequestMapping("/product")
public class ProductController {

	/**
	 * 1.查询所有在架的商品 2.获取类目type列表 3.查询类目 4.构造数据
	 */
	@GetMapping("/list")
	public ResultVO<ProductVo> list() {
		List<ProductVo> productVoList = new ArrayList<>();
		ProductVo p2 = new ProductVo();
		p2.setCategoryType(2);
		p2.setCategoryName("衣服");
		p2.setProductInfoVoList(
				Lists.newArrayList(new ProductInfoVo("2001", "寸衫", BigDecimal.valueOf(33), "", "cunshan.png")));
		productVoList.add(p2);

		ProductVo p1 = new ProductVo();
		p1.setCategoryType(1);
		p1.setCategoryName("日用百货");
		p1.setProductInfoVoList(
				Lists.newArrayList(new ProductInfoVo("1001", "锅", BigDecimal.valueOf(222), "", "guo.png")));
		productVoList.add(p1);
		return ResultVOUtil.success(productVoList);
	}

	@PostMapping("/listForOrder")
	public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {
		if (CollectionUtils.isEmpty(productIdList)) {
			return new ArrayList<ProductInfoOutput>();
		}
		List<ProductInfoOutput> ret = new ArrayList<ProductInfoOutput>();
		productIdList.stream().forEach(productId -> {
			ProductInfoOutput output = new ProductInfoOutput();
			output.setProductId(productId);
			output.setProductName("产品名称" + productId);
			ret.add(output);
		});
		return ret;
	}
}
